// 添加时间戳
function resetData(data) {
    data.baseInfo.sendTime = new Date().getTime()
    data.eventInfo.forEach(function (item) {
        item.sendTime = data.baseInfo.sendTime
    })
}

export function traceCustomEvent(options, eventInfo) {
    var baseInfo = {
        appCode: options.channelId,
        appName: "权益弹框",
        userUuid: options.userId,
        ext: {
            channelId: options.channelId,
            memberId: options.memberId,
            userId: options.userId,
            channelCode: options.channelCode,
            memberCode: options.memberCode,
        },
        device: navigator.userAgent
    }

    var eventType =  eventInfo.eventType
    eventInfo.eventType = eventType ? eventType : 'custom'
    eventInfo.title = document.title

    var data = {
        baseInfo,
        eventInfo: [eventInfo]
    }

    resetData(data)

    var xhr = new XMLHttpRequest();
    var method = "POST";
    xhr.open(method, options.baseUrl+`/tracing/send/${options.channelId}`);
    xhr.setRequestHeader("Content-type", "application/json;charset=utf-8");
    xhr.send(JSON.stringify(data));
}
